package Teacher;
import bean.SelectCourse;

import javax.swing.table.DefaultTableModel;
import java.util.List;

import static JDBCutil.JDBCUtil.modifySelectCourse;

public class CourseTableModel extends DefaultTableModel {
    private List<SelectCourse> list;

    public CourseTableModel(List<SelectCourse> list) {
        super(new Object[]{"课程号", "学生学号", "分数"}, 0);
        this.list = list;
        loadData(list);
    }

    private void loadData(List<SelectCourse> list) {
        for (int i = 0; i < list.size(); i++) {
            SelectCourse c = list.get(i);
            addRow(new Object[]{c.getCourseid(),c.getStudentid(),c.getMark()});
        }
    }

    @Override
    public boolean isCellEditable(int row, int column) {
        return column != 0; // Assuming ID column is not editable
    }

    @Override
    public void setValueAt(Object value, int row, int column) {
        super.setValueAt(value, row, column);
        SelectCourse course=list.get(row);

        course.setMark(Integer.parseInt((String) value));

        // Notify database update
        try {
            modifySelectCourse(course,course.getNum());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


}

